//
//  NewsDetailsViewController.m
//  Tiger
//
//  Created by King on 11-10-10.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NewsDetailsViewController.h"

#define k_PicUrl		@"picUrl"
#define k_Name			@"cateName"
#define k_DatedAt		@"updatedAt"
#define k_Title			@"title"
#define k_Content		@"content"

@implementation NewsDetailsViewController
@synthesize newsID;
@synthesize titleTextView;
@synthesize cateLable;
@synthesize dataLable;
@synthesize myImageView;
@synthesize activity;

@synthesize content;
@synthesize tableView = tableView_;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self.activity startAnimating];
	[self sendRequestPayInfo];
}

#pragma mark Private
- (void)sendRequestPayInfo
{
	//GET 请求
	NSString *getURL = [NSString stringWithFormat:@"http://www.speedtiger.com.cn/ios3/news_detail.html?id=%d", newsID];
	//NSLog(@"发送注册请求URL：%@", getURL);
	ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:getURL]];
	request.tag = -1;
	[request setValidatesSecureCertificate:NO];
	[request setDelegate:self];
	[request startAsynchronous];
}


#pragma mark ASIHTTPRequestDelegate methods
- (void)requestStarted:(ASIHTTPRequest *)request
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%@",[request responseString]);
	NSData *responseData = [request responseData];
	NSString *responseStr = [[[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding] autorelease];

	//NSLog(@"responseStr %@", responseStr);
	if (request.tag < 0) {

        NSDictionary *dict = [responseStr JSONValue];
		//NSLog(@"dict %@", dict);

		NSString *picURL = [dict objectForKey:k_PicUrl];
		if(picURL){
			NSURL *url=[NSURL URLWithString:picURL];
			[myImageView setImageWithURL:url];
			[self.activity stopAnimating]; 
		}
		titleTextView.font = [UIFont boldSystemFontOfSize:15];
		titleTextView.text = [dict objectForKey:k_Title];
		cateLable.text = [dict objectForKey:k_Name];
		dataLable.text = [dict objectForKey:k_DatedAt];
		content = [dict objectForKey:k_Content];
		//NSLog(@"content = %@",content);
		[self.tableView reloadData];
	}
	else {
		NSLog(@"recved bad data !");
	}
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	[self.activity stopAnimating]; 

	UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"温馨提示" 
													 message:@"无可用的网络连接" 
													delegate:self 
										   cancelButtonTitle:nil 
										   otherButtonTitles:nil] autorelease];
	[alert addButtonWithTitle:@"确定"];
	[alert show];
}


#pragma mark Table view methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//	
//	long long i = [content length];
//	if (i<30) {
		return 100;
//	}
//	return i*0.85;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{

    return 1;
	
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

	static NSString* CellIdentifier = @"Cellidentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
	//int i = [content length];
	
	UITextView *contentView;
	
//	if (i<30){
//		contentView = [[UITextView alloc] initWithFrame:CGRectMake(10, 270, 300, 50)];
//	}
	
//	contentView = [[UITextView alloc] initWithFrame:CGRectMake(10, 270, 300, i*0.85)];
	contentView = [[UITextView alloc] initWithFrame:CGRectMake(10, 270, 300, 120)];
	contentView.text = content;
	contentView.font = [UIFont systemFontOfSize:14];
	contentView.backgroundColor = [UIColor clearColor];
	contentView.textColor = [UIColor whiteColor];
	contentView.editable = NO;
	contentView.multipleTouchEnabled = NO;
	[self.tableView addSubview:contentView];
	
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	tableView.separatorStyle = NO;
	[contentView release];
	return cell;
}





- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.titleTextView = nil;
	self.cateLable = nil;
	self.dataLable = nil;
	self.myImageView = nil;
	self.activity = nil;
	self.tableView = nil;
}


- (void)dealloc {
    [super dealloc];
	[titleTextView release];
	[cateLable release];
	[dataLable release];
	[myImageView release];
	[activity release];
	[content release];
	[tableView_ release];
}


@end
